/* 对指定el节点追加class */
const addClass = (element, value) => {
  let newClassName
  if (!element.className) {
    element.className = value
  } else {
    newClassName = element.className
    if (!newClassName.split(' ').includes(value)) {
      newClassName += ' '
      newClassName += value
    }
    element.className = newClassName
  }
}

const removeClass = (element, value) => {
  let newClassName, newClassArr
  if (!element.className) {
    element.className = ''
  } else {
    newClassName = element.className
    newClassArr = newClassName.split(' ')
    if (newClassArr.indexOf(value) > -1) {
      const index = newClassArr.indexOf(value)
      newClassArr.splice(index, 1)
    }
    element.className = newClassArr.join(' ')
  }
}

export default {
  bind(el, binding, vnode, oldVnode) {
    const arrow_el = el.querySelector('.el-collapse-item__arrow')
    arrow_el.style.cssText += 'transform: rotate(0deg);'
    removeClass(arrow_el, 'el-icon-arrow-right')
    addClass(arrow_el, 'el-icon-d-arrow-left')

    vnode.child.handleFocus = () => {} // 清除elementUI 当前组件默认的点击事件方法
    vnode.child.state = false

    vnode.child.handleHeaderClick = () => {
      vnode.child.state = !vnode.child.state
      const rowVue = vnode.child.$parent.$parent.$parent.$parent.$children
      console.log(rowVue[0], rowVue[1])
      if (vnode.child.state) {
        removeClass(arrow_el, 'el-icon-d-arrow-left')
        addClass(arrow_el, 'el-icon-d-arrow-right')
        rowVue[0].$emit('colchange', 2)
      } else {
        removeClass(arrow_el, 'el-icon-d-arrow-right')
        addClass(arrow_el, 'el-icon-d-arrow-left')
        rowVue[0].$emit('colchange', 5)
      }
    } // 清除elementUI 当前组件默认的点击事件方法
  }
}
